<template>
  <Teleport to="body">
    <Transition name="loading-fade">
      <div v-if="visible" class="global-loading-overlay" :class="overlayClass">
        <div class="global-loading-container">
          <!-- 加载动画 -->
          <div class="loading-animation" :class="animationType">
            <!-- 默认旋转动画 -->
            <div v-if="animationType === 'spinner'" class="spinner">
              <div class="spinner-ring"></div>
            </div>
            
            <!-- 脉冲动画 -->
            <div v-else-if="animationType === 'pulse'" class="pulse">
              <div class="pulse-dot"></div>
              <div class="pulse-dot"></div>
              <div class="pulse-dot"></div>
            </div>
            
            <!-- 波浪动画 -->
            <div v-else-if="animationType === 'wave'" class="wave">
              <div class="wave-bar"></div>
              <div class="wave-bar"></div>
              <div class="wave-bar"></div>
              <div class="wave-bar"></div>
              <div class="wave-bar"></div>
            </div>
            
            <!-- 医疗主题动画 -->
            <div v-else-if="animationType === 'medical'" class="medical">
              <div class="medical-cross">
                <div class="cross-horizontal"></div>
                <div class="cross-vertical"></div>
              </div>
              <div class="medical-pulse"></div>
            </div>
            
            <!-- 自定义动画 -->
            <div v-else-if="animationType === 'custom'" class="custom-animation">
              <slot name="animation"></slot>
            </div>
          </div>
          
          <!-- 加载文本 -->
          <div v-if="showText" class="loading-text">
            <div class="loading-title">{{ title }}</div>
            <div v-if="subtitle" class="loading-subtitle">{{ subtitle }}</div>
            
            <!-- 进度条 -->
            <div v-if="showProgress" class="loading-progress">
              <div class="progress-bar">
                <div 
                  class="progress-fill" 
                  :style="{ width: `${progress}%` }"
                ></div>
              </div>
              <div class="progress-text">{{ progress }}%</div>
            </div>
            
            <!-- 加载提示 -->
            <div v-if="tips.length > 0" class="loading-tips">
              <Transition name="tip-fade" mode="out-in">
                <div :key="currentTip" class="tip-text">
                  {{ tips[currentTip] }}
                </div>
              </Transition>
            </div>
          </div>
          
          <!-- 取消按钮 -->
          <div v-if="showCancel" class="loading-actions">
            <el-button 
              type="text" 
              @click="handleCancel"
              class="cancel-button"
            >
              取消
            </el-button>
          </div>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<script setup>
import { ref, computed, watch, onMounted, onUnmounted } from 'vue'
import { ElButton } from 'element-plus'

const props = defineProps({
  // 是否显示
  visible: {
    type: Boolean,
    default: false
  },
  // 动画类型
  animationType: {
    type: String,
    default: 'spinner',
    validator: (value) => ['spinner', 'pulse', 'wave', 'medical', 'custom'].includes(value)
  },
  // 标题
  title: {
    type: String,
    default: '加载中...'
  },
  // 副标题
  subtitle: {
    type: String,
    default: ''
  },
  // 是否显示文本
  showText: {
    type: Boolean,
    default: true
  },
  // 是否显示进度条
  showProgress: {
    type: Boolean,
    default: false
  },
  // 进度值
  progress: {
    type: Number,
    default: 0,
    validator: (value) => value >= 0 && value <= 100
  },
  // 是否显示取消按钮
  showCancel: {
    type: Boolean,
    default: false
  },
  // 背景类型
  background: {
    type: String,
    default: 'blur',
    validator: (value) => ['blur', 'dark', 'light', 'transparent'].includes(value)
  },
  // 加载提示
  tips: {
    type: Array,
    default: () => []
  },
  // 提示切换间隔（毫秒）
  tipInterval: {
    type: Number,
    default: 3000
  },
  // 最小显示时间（毫秒）
  minDuration: {
    type: Number,
    default: 500
  },
  // 层级
  zIndex: {
    type: Number,
    default: 9999
  }
})

const emit = defineEmits(['cancel', 'timeout'])

const currentTip = ref(0)
let tipTimer = null
let minDurationTimer = null
const canClose = ref(false)

// 覆盖层样式类
const overlayClass = computed(() => ({
  [`background-${props.background}`]: true
}))

// 开始提示轮播
function startTipRotation() {
  if (props.tips.length <= 1) return
  
  tipTimer = setInterval(() => {
    currentTip.value = (currentTip.value + 1) % props.tips.length
  }, props.tipInterval)
}

// 停止提示轮播
function stopTipRotation() {
  if (tipTimer) {
    clearInterval(tipTimer)
    tipTimer = null
  }
}

// 处理取消
function handleCancel() {
  if (canClose.value) {
    emit('cancel')
  }
}

// 监听显示状态
watch(() => props.visible, (newVal) => {
  if (newVal) {
    canClose.value = false
    currentTip.value = 0
    
    // 开始提示轮播
    if (props.tips.length > 0) {
      startTipRotation()
    }
    
    // 最小显示时间
    if (props.minDuration > 0) {
      minDurationTimer = setTimeout(() => {
        canClose.value = true
      }, props.minDuration)
    } else {
      canClose.value = true
    }
  } else {
    stopTipRotation()
    if (minDurationTimer) {
      clearTimeout(minDurationTimer)
      minDurationTimer = null
    }
  }
})

// 监听提示数组变化
watch(() => props.tips, () => {
  if (props.visible && props.tips.length > 0) {
    stopTipRotation()
    currentTip.value = 0
    startTipRotation()
  }
}, { deep: true })

onMounted(() => {
  // 防止页面滚动
  if (props.visible) {
    document.body.style.overflow = 'hidden'
  }
})

onUnmounted(() => {
  stopTipRotation()
  if (minDurationTimer) {
    clearTimeout(minDurationTimer)
  }
  // 恢复页面滚动
  document.body.style.overflow = ''
})

// 监听visible变化来控制页面滚动
watch(() => props.visible, (newVal) => {
  if (newVal) {
    document.body.style.overflow = 'hidden'
  } else {
    document.body.style.overflow = ''
  }
})
</script>

<style scoped>
.global-loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: v-bind(zIndex);
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 背景样式 */
.background-blur {
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(8px);
}

.background-dark {
  background: rgba(0, 0, 0, 0.7);
}

.background-light {
  background: rgba(255, 255, 255, 0.9);
}

.background-transparent {
  background: transparent;
}

.global-loading-container {
  text-align: center;
  padding: 40px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  max-width: 400px;
  min-width: 200px;
}

/* 加载动画 */
.loading-animation {
  margin-bottom: 24px;
}

/* 旋转动画 */
.spinner {
  display: inline-block;
  position: relative;
  width: 60px;
  height: 60px;
}

.spinner-ring {
  width: 100%;
  height: 100%;
  border: 4px solid #f0f0f0;
  border-top: 4px solid #409eff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 脉冲动画 */
.pulse {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
}

.pulse-dot {
  width: 12px;
  height: 12px;
  background: #409eff;
  border-radius: 50%;
  animation: pulse-scale 1.4s ease-in-out infinite both;
}

.pulse-dot:nth-child(1) { animation-delay: -0.32s; }
.pulse-dot:nth-child(2) { animation-delay: -0.16s; }
.pulse-dot:nth-child(3) { animation-delay: 0s; }

@keyframes pulse-scale {
  0%, 80%, 100% {
    transform: scale(0);
  }
  40% {
    transform: scale(1);
  }
}

/* 波浪动画 */
.wave {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: 4px;
  height: 40px;
}

.wave-bar {
  width: 6px;
  background: #409eff;
  border-radius: 3px;
  animation: wave-bounce 1.2s ease-in-out infinite;
}

.wave-bar:nth-child(1) { animation-delay: -1.1s; }
.wave-bar:nth-child(2) { animation-delay: -1.0s; }
.wave-bar:nth-child(3) { animation-delay: -0.9s; }
.wave-bar:nth-child(4) { animation-delay: -0.8s; }
.wave-bar:nth-child(5) { animation-delay: -0.7s; }

@keyframes wave-bounce {
  0%, 40%, 100% {
    height: 10px;
  }
  20% {
    height: 40px;
  }
}

/* 医疗主题动画 */
.medical {
  position: relative;
  width: 60px;
  height: 60px;
  margin: 0 auto;
}

.medical-cross {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 30px;
  height: 30px;
}

.cross-horizontal,
.cross-vertical {
  position: absolute;
  background: #409eff;
  border-radius: 2px;
}

.cross-horizontal {
  width: 30px;
  height: 6px;
  top: 50%;
  left: 0;
  transform: translateY(-50%);
}

.cross-vertical {
  width: 6px;
  height: 30px;
  left: 50%;
  top: 0;
  transform: translateX(-50%);
}

.medical-pulse {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: 2px solid #409eff;
  border-radius: 50%;
  animation: medical-pulse 2s ease-in-out infinite;
}

@keyframes medical-pulse {
  0% {
    transform: scale(0.8);
    opacity: 1;
  }
  100% {
    transform: scale(1.2);
    opacity: 0;
  }
}

/* 文本样式 */
.loading-text {
  color: #333;
}

.loading-title {
  font-size: 18px;
  font-weight: 500;
  margin-bottom: 8px;
  color: #409eff;
}

.loading-subtitle {
  font-size: 14px;
  color: #666;
  margin-bottom: 16px;
}

/* 进度条 */
.loading-progress {
  margin: 16px 0;
}

.progress-bar {
  width: 100%;
  height: 6px;
  background: #f0f0f0;
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 8px;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #409eff, #67c23a);
  border-radius: 3px;
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 12px;
  color: #666;
}

/* 提示文本 */
.loading-tips {
  margin-top: 16px;
  min-height: 20px;
}

.tip-text {
  font-size: 13px;
  color: #888;
  line-height: 1.4;
}

/* 操作按钮 */
.loading-actions {
  margin-top: 20px;
}

.cancel-button {
  color: #666;
  font-size: 14px;
}

.cancel-button:hover {
  color: #409eff;
}

/* 过渡动画 */
.loading-fade-enter-active,
.loading-fade-leave-active {
  transition: opacity 0.3s ease;
}

.loading-fade-enter-from,
.loading-fade-leave-to {
  opacity: 0;
}

.tip-fade-enter-active,
.tip-fade-leave-active {
  transition: opacity 0.3s ease;
}

.tip-fade-enter-from,
.tip-fade-leave-to {
  opacity: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .global-loading-container {
    padding: 30px 20px;
    margin: 20px;
    min-width: auto;
  }
  
  .loading-title {
    font-size: 16px;
  }
  
  .loading-subtitle {
    font-size: 13px;
  }
}

/* 深色主题 */
@media (prefers-color-scheme: dark) {
  .global-loading-container {
    background: rgba(30, 30, 30, 0.95);
    border-color: rgba(255, 255, 255, 0.1);
  }
  
  .loading-text {
    color: #e0e0e0;
  }
  
  .loading-subtitle {
    color: #b0b0b0;
  }
  
  .tip-text {
    color: #a0a0a0;
  }
  
  .progress-bar {
    background: #404040;
  }
  
  .cancel-button {
    color: #b0b0b0;
  }
  
  .cancel-button:hover {
    color: #409eff;
  }
}
</style>